﻿Imports System.Windows.Forms

Public Class frmSelectDate

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub lstDate_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstDate.MouseDoubleClick, lstWindow.MouseDoubleClick
        btnApply.PerformClick()
    End Sub

    Private Sub frmSelectDate_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
        SaveSetting(REGAPP, Me.Name, "chkHideTimes", chkHideTimes.Checked)
    End Sub

    Private Sub frmSelectDate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GetWindowPos(REGAPP, Me)
        chkHideTimes.Checked = GetSetting(REGAPP, Me.Name, "chkHideTimes", False)
        FillLists()
    End Sub

    Private Sub FillLists()
        'find all unique date-times in all loaded series
        Dim lstDates As New Generic.List(Of Date)
        For Each ser As clsSeries In ActiveGraphTab.Series
            Dim StationID As String = "", PCode As String = ""
            Select Case ActiveGraphTab.GraphType
                Case enumGraphType.Depth
                    With CType(ser, clsSeriesDepthProfile)
                        StationID = .StationID
                        PCode = .PCode
                    End With
                Case enumGraphType.Width
                    With CType(ser, clsSeriesWidthProfile)
                        StationID = .StationID
                        PCode = .PCode
                    End With
                Case enumGraphType.Profile
                    With CType(ser, clsSeriesLongProfile)
                        StationID = .StationID
                        PCode = .PCode
                    End With
                Case enumGraphType.Scatter
                    With CType(ser, clsSeriesScatter)
                        StationID = .StationID
                        PCode = .PCode
                    End With
                Case Else
                    Debug.Assert("Invalid graph type")
            End Select
            Dim dr As DbDataReader = ser.Table.WRDBProject.DB.ExecuteReader("SELECT DISTINCT Date_Time from {0} WHERE Station_ID='{1}' AND PCode='{2}' AND Depth_M IS NOT NULL", ser.Table.TableName, StationID, PCode)
            While dr IsNot Nothing And dr.Read
                Dim dt As Date = dr.GetDateTime(0)
                If Not lstDates.Contains(dt) Then lstDates.Add(dt)
            End While
            dr.Close()
        Next
        lstDates.Sort()
        lstDate.Items.Clear()
        For Each dt As Date In lstDates
            Dim dtStr As String = dt.ToString(IIf(chkHideTimes.Checked, MyDateFormat, MyDateTimeFormat))
            If Not lstDate.Items.Contains(dtStr) Then lstDate.Items.Add(dtStr)
        Next
        lstWindow.Items.Clear()
        For Each s As String In [Enum].GetNames(GetType(enumTimeWindow))
            lstWindow.Items.Add(s.Replace("_", " ").Trim)
        Next
        With ActiveGraphTab.AnimationInfo
            lstDate.Text = .DateTime.ToString(IIf(chkHideTimes.Checked, MyDateFormat, MyDateTimeFormat))
            lstWindow.Text = CStr([Enum].GetName(GetType(enumTimeWindow), .TimeWindow)).Replace("_", " ").Trim
        End With
    End Sub

    Private Sub chkHideTimes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkHideTimes.CheckedChanged
        FillLists()
        If chkHideTimes.Checked Then lstWindow.Text = "12 Hours"
    End Sub

    Private Sub lst_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstDate.SelectedIndexChanged, lstWindow.SelectedIndexChanged
        If Not IsDate(lstDate.Text) Or lstWindow.SelectedIndex = -1 Then Exit Sub
        Me.Enabled = False
        With ActiveGraphTab.AnimationInfo
            .DateTime = CDate(lstDate.Text & IIf(chkHideTimes.Checked, " 12:00", ""))
            .TimeWindow = lstWindow.SelectedIndex
            ActiveGraphTab.SetSeriesDateTime(.DateTime)
            ActiveGraphTab.SetSeriesTimeWindow(.TimeWindow)
        End With
        Me.Enabled = True
    End Sub
End Class
